{ "cells": [ { "cell_type": "markdown", "id": "44ca09e5", "metadata": {}, "source": [ "# Programming Techniques — Laboratory\n", "## Input, Type Conversion and Strings\n", "\n", "This laboratory follows the order used in class, but the exercises deliberately **combine several concepts**.\n", "\n", "### Topics\n", "- `input()`\n", "- type conversion with `int()`, `float()` and `str()`\n", "- `int()` with a **base** (binary, hexadecimal, ...)\n", "- common conversion errors\n", "- `len()` with strings\n", "- character indexes\n", "- slicing, including negative steps\n", "- escape sequences\n", "- functions vs. methods\n", "- string methods: `.lower()`, `.upper()`, `.replace()`\n", "- f-strings\n", "\n", "### Working style\n", "1. **Guided exercise** — follow the instructor hands-on.\n", "2. **Laboratory exercises** — work semi-independently; ask questions and discuss your choices.\n", "3. **Independent review** — more complete exercises for consolidation and homework.\n", "\n", "> In this notebook we use strings as sequences. We do **not** use lists or loops yet." ] }, { "cell_type": "markdown", "id": "4d29ecaf", "metadata": {}, "source": [ "---\n", "# Part B — Laboratory\n", "\n", "Work on the following exercises semi-independently.\n", "\n", "You are encouraged to test small expressions, compare results, ask questions, and discuss alternative solutions." ] }, { "cell_type": "markdown", "id": "96cbe770", "metadata": {}, "source": [ "## 2. Product code inspector\n", "\n", "### Theory recall\n", "\n", "A string is an **ordered sequence of characters**.\n", "\n", "```python\n", "code = \"LIS-2026-A7\"\n", "\n", "len(code) # number of characters\n", "code[0] # first character\n", "code[-1] # last character\n", "```\n", "\n", "Python indexes start from `0`. Negative indexes count from the end.\n", "\n", "### Task\n", "\n", "A company uses product identifiers with this format:\n", "\n", "```text\n", "LIS-2026-A7\n", "```\n", "\n", "where:\n", "- `LIS` is the office code;\n", "- `2026` is the year;\n", "- `A7` is the product family.\n", "\n", "Ask the user to enter a code in this format and print:\n", "1. the complete code;\n", "2. its total length;\n", "3. the first character;\n", "4. the last character;\n", "5. the office code;\n", "6. the year;\n", "7. the product family.\n", "\n", "Use **indexes/slicing** and an **f-string**.\n", "\n", "Hint:\n", "\n", "```python\n", "text[start:stop]\n", "```\n", "\n", "The `stop` position is **excluded**." ] }, { "cell_type": "code", "execution_count": null, "id": "90728f44", "metadata": {}, "outputs": [], "source": [ "product_code = input(\"Product code (e.g. LIS-2026-A7): \")\n", "\n", "# TODO: inspect the code using len(), indexes and slicing" ] }, { "cell_type": "markdown", "id": "4a654b58", "metadata": {}, "source": [ "## 3. Reading a code forwards and backwards\n", "\n", "### Theory recall\n", "\n", "Slicing can include a third parameter, the **step**:\n", "\n", "```python\n", "text[start:stop:step]\n", "```\n", "\n", "Examples:\n", "\n", "```python\n", "text[::2] # every second character\n", "text[::-1] # the whole string in reverse order\n", "```\n", "\n", "A negative step means that Python moves **from right to left**.\n", "\n", "### Task\n", "\n", "Ask for a tracking code and display:\n", "- the original code;\n", "- the code reversed;\n", "- characters in even index positions: `0, 2, 4, ...`;\n", "- characters in odd index positions: `1, 3, 5, ...`;\n", "- characters from index `6` down to index `2` (index `2` excluded).\n", "\n", "Use only slicing." ] }, { "cell_type": "code", "execution_count": null, "id": "afe7545f", "metadata": {}, "outputs": [], "source": [ "tracking = input(\"Tracking code: \")\n", "\n", "# TODO: use slicing with positive and negative steps" ] }, { "cell_type": "markdown", "id": "bf9d3469", "metadata": {}, "source": [ "## 4. Mini sales histogram\n", "\n", "### Theory recall\n", "\n", "Escape sequences represent special characters inside strings:\n", "\n", "```python\n", "\"\\n\" # new line\n", "\"\\t\" # horizontal tab\n", "\"\\\\\" # backslash\n", "```\n", "\n", "Strings can also be repeated:\n", "\n", "```python\n", "\"*\" * 5 # \"*****\"\n", "```\n", "\n", "### Task\n", "\n", "A shop records the number of items sold in three time slots.\n", "\n", "Ask for:\n", "- morning sales;\n", "- afternoon sales;\n", "- evening sales.\n", "\n", "Convert each value to `int` and print a simple horizontal histogram.\n", "\n", "Example:\n", "\n", "```text\n", "SALES REPORT\n", "Morning | ****\n", "Afternoon | *******\n", "Evening | **\n", "\n", "Total items sold: 13\n", "```\n", "\n", "Requirements:\n", "- use `input()` and `int()`;\n", "- use `\"*\" * value`;\n", "- use `\\n` and/or `\\t`;\n", "- use at least one f-string.\n", "\n", "No loops are needed." ] }, { "cell_type": "code", "execution_count": null, "id": "9f02772f", "metadata": {}, "outputs": [], "source": [ "# TODO: read the three values\n", "# TODO: calculate the total\n", "# TODO: print the histogram" ] }, { "cell_type": "markdown", "id": "473145e8", "metadata": {}, "source": [ "## 5. Functions vs. methods — customer message formatter\n", "\n", "### Theory recall\n", "\n", "A **function** is called by name:\n", "\n", "```python\n", "len(text)\n", "```\n", "\n", "A **method** is associated with an object and is called with dot notation:\n", "\n", "```python\n", "text.upper()\n", "```\n", "\n", "Useful string methods:\n", "\n", "```python\n", "text.lower()\n", "text.upper()\n", "text.replace(old, new)\n", "```\n", "\n", "Strings are **immutable**: these methods return a new string; they do not change the original one.\n", "\n", "### Task\n", "\n", "Ask the user for a customer-service message and print:\n", "1. the original message;\n", "2. its length;\n", "3. a lowercase version;\n", "4. an uppercase version;\n", "5. a version where every occurrence of `\"urgent\"` is replaced with `\"priority\"`.\n", "\n", "Try first with:\n", "\n", "```text\n", "urgent order: urgent delivery requested\n", "```\n", "\n", "Use `len()` as a **function**, and the transformations as **methods**." ] }, { "cell_type": "code", "execution_count": null, "id": "8ebae306", "metadata": {}, "outputs": [], "source": [ "message = input(\"Customer message: \")\n", "\n", "# TODO: use len()\n", "# TODO: use lower(), upper() and replace()\n", "# TODO: print the results clearly" ] }, { "cell_type": "markdown", "id": "6000d04b", "metadata": {}, "source": [ "## 6. Conversion detective\n", "\n", "### Theory recall\n", "\n", "A conversion succeeds only if the string is compatible with the requested type/base.\n", "\n", "```python\n", "int(\"42\") # OK\n", "float(\"3.14\") # OK\n", "int(\"1011\", 2) # OK\n", "int(\"2F\", 16) # OK\n", "```\n", "\n", "### Task\n", "\n", "Before running the cell below, predict which expressions work and which fail.\n", "\n", "For each failing expression:\n", "1. identify **why** it fails;\n", "2. propose a corrected expression.\n", "\n", "> Run the expressions **one at a time**: execution stops at the first uncaught error." ] }, { "cell_type": "code", "execution_count": null, "id": "02a4563b", "metadata": {}, "outputs": [], "source": [ "# Uncomment ONE expression at a time.\n", "\n", "# print(int(\"25\"))\n", "# print(int(\"3.14\"))\n", "# print(float(\"3.14\"))\n", "# print(int(\"1011\", 2))\n", "# print(int(\"102\", 2))\n", "# print(int(\"2F\", 16))\n", "# print(int(\"2G\", 16))\n", "# print(str(125))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }